/**
  ******************************************************************************
  * @file    Task.c
  * @author  Qilong Huang
  * @version V0.0.1
  * @date    04-03-2020
  * @brief   This file contains functions to initialize the OS.  
  *				 
  *          
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; KION(SZ) R&D Center
  *
  * Licensed under KION, (the "License");
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/

#include "task.h"
#include "global.h" 
#include "CAN.h"
#include "spi.h"
#include "bdi.h"
#include "timex.h"
#include "swidft.h"

      
const  int app_vert_crc __attribute__ ((at(0x08008188))) = 0; 
 

/* Functions ------------------------------------------------------------------*/
extern void display_task(void const *argument); 
osThreadDef (display_task, osPriorityBelowNormal, 1, DISPLAY_TASK_STACK_LENGTH);       // thread object
extern void ledicons_task(void const *argument); 
osThreadDef (ledicons_task, osPriorityNormal, 1, 0);                                   // thread object
extern void outputs_task(void const *argument);
osThreadDef (outputs_task, osPriorityNormal, 1, 0);                                    // thread object
extern void BDI_task(void const *argument); 
osThreadDef (BDI_task, osPriorityNormal, 1, 0);                                        // thread object
extern void BDI_Meas_task(void const *argument); 
osThreadDef (BDI_Meas_task, osPriorityNormal, 1, 0);                                        // thread object
extern void TOD_task(void const *argument); 
osThreadDef (TOD_task, osPriorityNormal, 1, 0);                                        // thread object
extern void PWM_task(void const *argument); 
osThreadDef (PWM_task, osPriorityNormal, 1, 0);                                        // thread object
extern void CAN1_task(void const *argument); 
osThreadDef (CAN1_task, osPriorityAboveNormal, 1, CAN_TASK_STACK_LENGTH);              // thread object
 
extern void switch_inputs_task(void const *argument); 
osThreadDef (switch_inputs_task, osPriorityNormal, 1, 0);                              // thread object
extern void analog_inputs_task(void const *argument);
osThreadDef (analog_inputs_task, osPriorityNormal, 1, 0);                              // thread object

extern void sample_PB_task(void const *argument); 
osThreadDef (sample_PB_task, osPriorityNormal, 1, 0);                                  // thread object

extern void hourmeters_task(void const *argument); 
osThreadDef (hourmeters_task, osPriorityNormal, 1, 0);                                 // thread object

extern void eeprom_task(void const *argument); 
osThreadDef (eeprom_task, osPriorityNormal, 1, 0);                              // thread object

extern void iwatchdog_task(void const *argument); 
osThreadDef (iwatchdog_task, osPriorityNormal, 1, 0);                                     // thread object

/**
* @brief	It creates and starts all other tasks. 
* @param	none
* @note     none
* @retval   none
*/
void create_tasks(void) 
{
	//peripherals init 
	LCD_init();				//initialize monochrome 128x64 display
	AnalogInputs_Init();
	EE_SPI_Init();
	load_parameters_from_ee();
	IWDG_ReloadCounter();

	CAN_Config();

    swidft_software_id_from_time( mcu_get_build_time_app(),software_id );

	osThreadCreate(osThread(CAN1_task), NULL);	

	osThreadCreate(osThread(ledicons_task), NULL);	
	osThreadCreate(osThread(outputs_task), NULL);	
	osThreadCreate(osThread(BDI_task), NULL);	
    osThreadCreate(osThread(BDI_Meas_task), NULL);
	osThreadCreate(osThread(TOD_task), NULL);	
	osThreadCreate(osThread(PWM_task), NULL);
	osThreadCreate(osThread(switch_inputs_task), NULL);	
	osThreadCreate(osThread(analog_inputs_task), NULL);	
	osThreadCreate(osThread(sample_PB_task), NULL);
	osThreadCreate(osThread(hourmeters_task), NULL);	
	osThreadCreate(osThread(eeprom_task), NULL);	
	osThreadCreate(osThread(display_task), NULL);
	osThreadCreate(osThread(iwatchdog_task), NULL);	
//	
//	//turn on all the six LED outputs,then turn off it.
	IWDG_ReloadCounter();	/* Reload IWDG counter */
}


